<?php

namespace app\modules\Product\controllers;

use Yii;
use app\modules\Product\models\Invoice;
use app\modules\Product\models\InvoiceSearch;
use app\modules\Product\models\Model;
use app\modules\Product\models\InvoiceDetail;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;

/**
 * Invoice 管理
 */
class InvoiceController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    //Invoice列表
    public function actionIndex($product_id)
    {
        $searchModel = new InvoiceSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$product_id);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'product_id' => $product_id
        ]);
    }

    //详情
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    //创建
    public function actionCreate($product_id)
    {
        $model = new Invoice();

        $detail = [new InvoiceDetail()];

        if ($model->load(Yii::$app->request->post())) {

            $detail = Model::createMultiple(InvoiceDetail::classname());
            $valid = $model->validate();
            $valid = Model::loadMultiple($detail, Yii::$app->request->post()) && Model::validateMultiple($detail);

             if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    $price = 0;
                    if($detail){
                        foreach($detail as $d){
                            $price += $d->num * $d->s_price; //总金额
                        }
                    }  
                    $model->price = $price;
                    if($flag = $model->save(false)){
                        foreach ($detail as $rp) {
                            $rp->invoice_id = $model->id;
                            $rp->t_price = $rp->num * $rp->s_price;
                            if (! ($flag = $rp->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }    

                   }
                   if ($flag) {
                        $transaction->commit();
                        return $this->redirect(['view','id'=>$model->id]);
                    }

                } catch (Exception $e) {
                    $transaction->rollBack();
                    echo '错误';
                }   
            }

        }else{
            $model->product_id = $product_id;
            $model->pay_type = 1;
            return $this->render('create', [
                'model' => $model,
                'detail' => $detail,
            ]);
        }

       
    }

    //更新
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        $detail = $model->invoiceDetail;

        if(!$detail){
            $detail = [new InvoiceDetail()];
        }

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            $oldIDs = ArrayHelper::map($detail, 'id', 'id');
            $detail = Model::createMultiple(InvoiceDetail::classname(),$detail);
            Model::loadMultiple($detail, Yii::$app->request->post());
            
            $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($detail, 'id', 'id')));
                
            $valid = $model->validate();
            $valid = Model::validateMultiple($detail) && $valid;

            if ($valid) {
                    $transaction = \Yii::$app->db->beginTransaction();
                    try {

                        $price = 0;
                        if($detail){
                            foreach($detail as $d){
                                $price += $d->num * $d->s_price;
                            }
                        }
                        $model->price = $price;  
                        if($flag = $model->save(false)){
                            if (! empty($deletedIDs)) {
                                InvoiceDetail::deleteAll(['id' => $deletedIDs]);
                            }
                            foreach ($detail as $rp) {
                                $rp->invoice_id = $model->id;
                                $rp->t_price = $rp->num * $rp->s_price;
                                if (! ($flag = $rp->save(false))) {
                                    $transaction->rollBack();
                                    break;
                                }
                            }    

                       } 
                       
                       if ($flag) {
                            $transaction->commit();
                            return $this->redirect(['view','id'=>$model->id]);
                        }

                    } catch (Exception $e) {
                        $transaction->rollBack();
                        echo '错误';
                    }   
            }

        }else{

            return $this->render('update', [
                'model' => $model,
                'detail' => $detail,
            ]);
        }

    }

    //下载Invoice 
    public  function actionDownload($id){

        $model = $this->findModel($id);

        if(!$model){
            return false;
        }

        //开始//
    
        $objPHPExcel = new \PHPExcel();
        $sheet = $objPHPExcel->getActiveSheet(0);
        $sheet->setTitle('Invoice');

        //设置默认字体
        $sheet->getDefaultStyle()->getFont()->setSize(10)->setName('Arial');

        //设置默认背景颜色
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFill()->getStartColor()->setARGB('003300');

        //设置表格宽度
        $sheet->getColumnDimension('A')->setWidth(2);
        $sheet->getColumnDimension('B')->setWidth(4);
        $sheet->getColumnDimension('K')->setWidth(16);
        $sheet->getColumnDimension('L')->setWidth(16);
        $sheet->getColumnDimension('M')->setWidth(4);

        //设置表格高度
        $sheet->getRowDimension(1)->setRowHeight(8);
        $sheet->getRowDimension(2)->setRowHeight(15);
        $sheet->getRowDimension(8)->setRowHeight(20);
        $sheet->getRowDimension(7)->setRowHeight(30);
        $sheet->getRowDimension(10)->setRowHeight(40);

        //边框样式
        $style_all = ['borders' => ['allborders' => [ 'style' => \PHPExcel_Style_Border::BORDER_THIN,'color' => ['argb' => '000000'], ],],];
        $style_outline = ['borders' => ['outline' => [ 'style' => \PHPExcel_Style_Border::BORDER_THIN,'color' => ['argb' => '000000'], ],],];
        $style_outline_dash = ['borders' => ['outline' => [ 'style' => \PHPExcel_Style_Border::BORDER_DASHED,'color' => ['argb' => '000000'], ],],];
        $style_bottom_thin = ['borders' => ['bottom' => [ 'style' => \PHPExcel_Style_Border::BORDER_THIN,'color' => ['argb' => 'C0C0C0'], ],],];
        $style_bottom_dash = ['borders' => ['bottom' => [ 'style' => \PHPExcel_Style_Border::BORDER_DASHED,'color' => ['argb' => 'C0C0C0'], ],],];
        $style_bottom_thin = ['borders' => ['bottom' => [ 'style' => \PHPExcel_Style_Border::BORDER_THIN,'color' => ['argb' => 'C0C0C0'], ],],];
        $style_top_thick_blue = ['borders' => ['top' => [ 'style' => \PHPExcel_Style_Border::BORDER_THICK,'color' => ['argb' => '3366FF'], ],],];
        $style_bottom_thin_black = ['borders' => ['bottom' => [ 'style' => \PHPExcel_Style_Border::BORDER_THIN,'color' => ['argb' => '000000'], ],],];
        $style_bottom_dash_black = ['borders' => ['bottom' => [ 'style' => \PHPExcel_Style_Border::BORDER_DASHED,'color' => ['argb' => '000000'], ],],];
        $style_right_dash_black = ['borders' => ['right' => [ 'style' => \PHPExcel_Style_Border::BORDER_DASHED,'color' => ['argb' => '000000'], ],],];
        $style_right_thin_black = ['borders' => ['right' => [ 'style' => \PHPExcel_Style_Border::BORDER_THIN,'color' => ['argb' => '000000'], ],],];
        $style_top = ['borders' => ['top' => [ 'style' => \PHPExcel_Style_Border::BORDER_THICK,'color' => ['argb' => 'C0C0C0'], ],],];
        $style_bottom = ['borders' => ['bottom' => [ 'style' => \PHPExcel_Style_Border::BORDER_THICK,'color' => ['argb' => 'C0C0C0'], ],],];
        $style_left = ['borders' => ['left' => [ 'style' => \PHPExcel_Style_Border::BORDER_THICK,'color' => ['argb' => 'C0C0C0'], ],],];
        $style_right = ['borders' => ['right' => [ 'style' => \PHPExcel_Style_Border::BORDER_THICK,'color' => ['argb' => 'C0C0C0'], ],],];

        //空一行
        $sheet->mergeCells("B2:L2"); 

        //图片invoice
        $sheet->mergeCells("C3:F6"); 
        $objDrawing = new \PHPExcel_Worksheet_Drawing();
        $objDrawing->setPath("./img/invoice.png");
        $objDrawing->setHeight(630);
        $objDrawing->setWidth(210);
        $objDrawing->setOffsetX(1);                       
        $objDrawing->setOffsetY(1); 
        $objDrawing->setCoordinates('C3');
        $objDrawing->setWorksheet($sheet);

        //默认信息
        $sheet->mergeCells("G3:J6"); 
        $str = "CHOOSE A DESTINATION \n5800 S Eastern Ave, Suite 330, Commerce, CA 90040 \nTel：(310) -988-7571 ";
        $sheet->getStyle('G3')->getAlignment()->setWrapText(true);
        $sheet->setCellValue('G3',$str);

        //invoice 编号
        $sheet->setCellValue('K3','Invoice No.');
        $sheet->setCellValue('L3',$model->code);
        $sheet->getStyle("K3")->getFont()->setSize(10)->setBold(true)->setName('Arial')->getColor()->setARGB('000000');
        $sheet->getStyle("K3")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $sheet->getStyle("L3")->getFont()->setSize(10)->setBold(true)->setName('Arial')->getColor()->setARGB('FF0000');
        //Invoice标题
        $sheet->setCellValue('K7','INVOICE');
        $sheet->getStyle("K7")->getFont()->setSize(18)->setBold(true)->setItalic(true)->setName('Arial')->getColor()->setARGB('000000');
        $sheet->getStyle("K7")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        //设置下边框
        $sheet->getStyle('L4:L4')->applyFromArray($style_bottom_thin);
        $sheet->getStyle('L5:L5')->applyFromArray($style_bottom_thin);
        $sheet->getStyle('L6:L6')->applyFromArray($style_bottom_thin);

        //空一行
        $sheet->mergeCells("C7:J7"); 

        //customer具体信息
        $sheet->mergeCells("C8:J8");
        $sheet->mergeCells("D9:J9");
        $sheet->mergeCells("D10:J10");
        $sheet->mergeCells("D10:J10");
        $sheet->mergeCells("D11:E11");
        $sheet->mergeCells("I11:J11");
        $sheet->mergeCells("D12:J12");
        $sheet->setCellValue('C8','   Customer');

        $sheet->getStyle("C8")->getFont()->setSize(10)->setBold(true)->setItalic(true)->setName('Arial')->getColor()->setARGB('000000');
        $sheet->getStyle("C8")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        $sheet->setCellValue('C9','Name');
        $sheet->setCellValue('C10','Address');
        $sheet->setCellValue('C11','City');
        $sheet->setCellValue('F11','State');
        $sheet->setCellValue('H11','ZIP');
        $sheet->setCellValue('C12','Phone');


        $sheet->setCellValue('D9',$model->product->customer->en_name);
        $sheet->setCellValue('D10',$model->product->customer->en_address);

        $sheet->setCellValue('K9','Date');
        $sheet->setCellValue('K10','Order No.');
        $sheet->setCellValue('K11','Rep');
        $sheet->setCellValue('K12','FOB');
        $sheet->setCellValue('L9',date('Y/m/d',time()));

        //customer信息的边框
        $sheet->getStyle('C8:J12')->applyFromArray($style_outline);
        $sheet->getStyle('K8:L12')->applyFromArray($style_outline);

        $sheet->getStyle('D9:J9')->applyFromArray($style_bottom_dash);
        $sheet->getStyle('D10:J10')->applyFromArray($style_bottom_dash);
        $sheet->getStyle('D11:E11')->applyFromArray($style_bottom_dash);
        $sheet->getStyle('G11:G11')->applyFromArray($style_bottom_dash);
        $sheet->getStyle('I11:J11')->applyFromArray($style_bottom_dash);

        $sheet->getStyle('L9:L9')->applyFromArray($style_bottom_dash);
        $sheet->getStyle('L10:L10')->applyFromArray($style_bottom_dash);
        $sheet->getStyle('L11:L11')->applyFromArray($style_bottom_dash);

        //invoice 具体的数量、价格、备注等信息
        $sheet->mergeCells("D13:J13");
        $sheet->mergeCells("D14:J14");
        $sheet->setCellValue('C14','Qty');
        $sheet->setCellValue('D14','Description');
        $sheet->setCellValue('K14','Unit Price');
        $sheet->setCellValue('L14','TOTAL');

        $sheet->getStyle("C14:L14")->getFont()->setSize(10)->setBold(true)->setName('Arial')->getColor()->setARGB('000000');

        $key = 0;
        if($model->invoiceDetail){
            foreach($model->invoiceDetail as $ind){
                $sheet->mergeCells(sprintf("D%d:J%d",$key+15,$key+15));
                $sheet->setCellValue(sprintf("C%d",$key+15),$ind->num);
                $sheet->setCellValue(sprintf("D%d",$key+15),$ind->remark);
                $sheet->setCellValue(sprintf("K%d",$key+15),$ind->s_price);
                //设置格式为美元
                $sheet->getStyle(sprintf("K%d",$key+15))->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);

                $sheet->setCellValue(sprintf("L%d",$key+15),$ind->t_price);
                $sheet->getStyle(sprintf("L%d",$key+15))->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);

                $sheet->getStyle(sprintf("L%d",$key+15))->applyFromArray($style_bottom_thin);
                $sheet->getStyle(sprintf("C%d:K%d",$key+15,$key+15))->applyFromArray($style_bottom_thin);

                $key++;
            }
        }

        $num_arr = [15,16,17,18,19,20,21,22,23,24,25,26,27,28,29];
        foreach($num_arr as $nums){
            $sheet->mergeCells(sprintf("D%d:J%d",$key+$nums,$key+$nums));
            $sheet->getStyle(sprintf("L%d",$key+$nums))->applyFromArray($style_bottom_thin);
        }

        $sheet->setCellValue(sprintf("D%d",$key+18),'Bank Information:');
        $sheet->setCellValue(sprintf("D%d",$key+19),'Wire routing No: 026 009 593');
        $sheet->setCellValue(sprintf("D%d",$key+20),'Swift code:BOFAUS3N ');
        $sheet->setCellValue(sprintf("D%d",$key+21),'Bank Account:501017234180');
        $sheet->setCellValue(sprintf("D%d",$key+22),'Bank Name: Bank of America');
        $sheet->setCellValue(sprintf("D%d",$key+23),'Address:6900 Westcliff Drive - 4th Floor Las Vegas, Nevada 89145, USA');
        $sheet->setCellValue(sprintf("D%d",$key+24),'Beneficiary Name: Choose A Destination LLC');
        $sheet->setCellValue(sprintf("D%d",$key+25),'Beneficiary Add:3525 W. Hacienda Avenue, Las Vegas, NV  89118');
         
        $sheet->getRowDimension($key+26)->setRowHeight(15);//设置高度
         
        $sheet->setCellValue(sprintf("D%d",$key+27),sprintf("Tour code:%s",$model->product->code));
        $sheet->getStyle(sprintf("D%d",$key+27))->getFont()->setSize(10)->setBold(true)->setName('Arial')->getColor()->setARGB('000000');



        //invoice 具体的数量、价格、备注等信息的边框颜色
        $sheet->getStyle("C14:L14")->applyFromArray($style_bottom_thin_black);
        $sheet->getStyle(sprintf("C14:C%d",$key+29))->applyFromArray($style_right_thin_black);
        $sheet->getStyle(sprintf("J14:J%d",$key+29))->applyFromArray($style_right_thin_black);
        $sheet->getStyle(sprintf("K14:K%d",$key+29))->applyFromArray($style_right_thin_black);

        //空2行

        //具体信息设置边框
        $sheet->getStyle(sprintf("C14:L%d",$key+29))->applyFromArray($style_outline);

        //具体信息设置字体和字体格式
        $sheet->getStyle(sprintf("C14:L%d",$key+15))->getFont()->setSize(10)->setName('Arial');
        $sheet->getStyle(sprintf("C14:L%d",$key+15))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        //payment Detail
        $sheet->setCellValue(sprintf("D%d",$key+32),'  Cash');
        $sheet->setCellValue(sprintf("D%d",$key+33),'  Wire');
        $sheet->setCellValue(sprintf("D%d",$key+34),'  Credit Card');

        $sheet->setCellValue(sprintf("C%d",$key+35),'Name');
        $sheet->setCellValue(sprintf("C%d",$key+36),'CC #');
        $sheet->setCellValue(sprintf("D%d",$key+37),'Expires');
        $sheet->mergeCells(sprintf("D%d:F%d",$key+35,$key+35));
        $sheet->mergeCells(sprintf("D%d:F%d",$key+36,$key+36));
        $sheet->mergeCells(sprintf("E%d:F%d",$key+37,$key+37));
        $sheet->getStyle(sprintf("D%d:F%d",$key+35,$key+35))->applyFromArray($style_bottom_dash_black);
        $sheet->getStyle(sprintf("D%d:F%d",$key+36,$key+36))->applyFromArray($style_bottom_dash_black);
        $sheet->getStyle(sprintf("E%d:F%d",$key+37,$key+37))->applyFromArray($style_bottom_dash_black);

        $sheet->getStyle(sprintf("K%d:L%d",$key+30,$key+34))->applyFromArray($style_outline);
        $sheet->getStyle(sprintf("C%d:G%d",$key+31,$key+38))->applyFromArray($style_outline);


        $sheet->setCellValue(sprintf("K%d",$key+30),'SubTotal');
        $sheet->setCellValue(sprintf("K%d",$key+32),'Taxes');
        $sheet->setCellValue(sprintf("K%d",$key+34),'TOTAL');

        $sheet->setCellValue(sprintf("L%d",$key+30),"=SUM(".sprintf("L%d",15).":".sprintf("L%d",$key+29).')');
        $sheet->setCellValue(sprintf("L%d",$key+34),"=SUM(".sprintf("L%d",15).":".sprintf("L%d",$key+29).')');
        $sheet->getStyle(sprintf("L%d",$key+30))->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
        $sheet->getStyle(sprintf("L%d",$key+34))->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);


        $sheet->getStyle(sprintf("K%d:K%d",$key+30,$key+34))->getFont()->setSize(10)->setBold(true)->setName('Arial')->getColor()->setARGB('000000');
        $sheet->getStyle(sprintf("K%d:K%d",$key+30,$key+34))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        $sheet->getStyle(sprintf("K%d:K%d",$key+30,$key+34))->applyFromArray($style_right_dash_black);
        $sheet->getStyle(sprintf("L%d:L%d",$key+30,$key+30))->applyFromArray($style_bottom_dash_black);
        $sheet->getStyle(sprintf("L%d:L%d",$key+31,$key+31))->applyFromArray($style_bottom_dash_black);
        $sheet->getStyle(sprintf("L%d:L%d",$key+32,$key+32))->applyFromArray($style_bottom_dash_black);
        $sheet->getStyle(sprintf("L%d:L%d",$key+33,$key+33))->applyFromArray($style_bottom_dash_black);

        $sheet->mergeCells(sprintf("K%d:L%d",$key+36,$key+38));
        $sheet->setCellValue(sprintf("K%d",$key+36),'Office Use Only');
        $sheet->getStyle(sprintf("K%d",$key+36))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_TOP);
        $sheet->getStyle(sprintf("K%d:L%d",$key+36,$key+38))->applyFromArray($style_outline_dash);

        

        //底部备注
        $sheet->mergeCells(sprintf("D%d:K%d",$key+40,$key+43));
        $sheet->setCellValue(sprintf("D%d",$key+40),'Please make a check payable to Choose A Destination');
        $sheet->mergeCells(sprintf("D%d:K%d",$key+45,$key+47));
        $sheet->setCellValue(sprintf("D%d",$key+45),'Thank you for your business.');

        $sheet->getStyle(sprintf("D%d:D%d",$key+40,$key+45))->getFont()->setSize(10)->setItalic(true)->setName('Arial')->getColor()->setARGB('000000');
        $sheet->getStyle(sprintf("D%d:D%d",$key+40,$key+45))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);


        //设置Invoice整个表格的背景颜色为白色
        $objPHPExcel->getActiveSheet()->getStyle(sprintf("B2:M%d",$key+47))->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $objPHPExcel->getActiveSheet()->getStyle(sprintf("B2:M%d",$key+47))->getFill()->getStartColor()->setARGB('FFFFFF');

        // //invoice编号的背景颜色为黄色
        // $objPHPExcel->getActiveSheet()->getStyle("L3:L3")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        // $objPHPExcel->getActiveSheet()->getStyle("L3:L3")->getFill()->getStartColor()->setARGB('FFFF00');

        // //invoice Date 的背景颜色为黄色
        // $objPHPExcel->getActiveSheet()->getStyle("L9:L9")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        // $objPHPExcel->getActiveSheet()->getStyle("L9:L9")->getFill()->getStartColor()->setARGB('FFFF00');

        //设置Invoice整个表格的边框 
        $sheet->getStyle('B2:M2')->applyFromArray($style_top);
        $sheet->getStyle(sprintf("M2:M%d",$key+47))->applyFromArray($style_right);
        $sheet->getStyle(sprintf("B2:B%d",$key+47))->applyFromArray($style_left);
        $sheet->getStyle(sprintf("B%d:M%d",$key+47,$key+47))->applyFromArray($style_bottom);
        
        //设置底部信息一行表格的顶部边框为蓝色
        $sheet->getStyle(sprintf("C%d:L%d",$key+44,$key+44))->applyFromArray($style_top_thick_blue);
        $sheet->getStyle(sprintf("C%d:L%d",$key+44,$key+44))->applyFromArray($style_bottom_thin_black);
        $sheet->getRowDimension($key+44)->setRowHeight(4);
       
   
        // //设置背景色为黄色
        // $objPHPExcel->getActiveSheet()->getStyle(sprintf("C%d:J%d",$key+30,$key+39))->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        // $objPHPExcel->getActiveSheet()->getStyle(sprintf("C%d:J%d",$key+30,$key+39))->getFill()->getStartColor()->setARGB('FFFF00');

        // $objPHPExcel->getActiveSheet()->getStyle("D9:J12")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        // $objPHPExcel->getActiveSheet()->getStyle("D9:J12")->getFill()->getStartColor()->setARGB('FFFF00');

        // //Tour code 背景色为黄色
        // $objPHPExcel->getActiveSheet()->getStyle(sprintf("D%d:D%d",$key+27,$key+27))->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        // $objPHPExcel->getActiveSheet()->getStyle(sprintf("D%d:D%d",$key+27,$key+27))->getFill()->getStartColor()->setARGB('FFFF00');


        // //设置invoice 详细信息背景色为黄色
        // if($key>0){
        //     for($i=1;$i<=$key;$i++){
        //         $objPHPExcel->getActiveSheet()->getStyle(sprintf("C%d:K%d",$i+14,$i+14))->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        //         $objPHPExcel->getActiveSheet()->getStyle(sprintf("C%d:K%d",$i+14,$i+14))->getFill()->getStartColor()->setARGB('FFFF00');
        //     }
        // }

        //Office Use Only 背景色
        $objPHPExcel->getActiveSheet()->getStyle(sprintf("K%d:L%d",$key+36,$key+38))->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $objPHPExcel->getActiveSheet()->getStyle(sprintf("K%d:L%d",$key+36,$key+38))->getFill()->getStartColor()->setARGB('E3E3E3');

        //结束//

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $fileName = sprintf("%s.xlsx",uniqid());
        $path = 'uploads/invoice/'.date('Ym').'/';
        if(!is_dir($path)){  
              mkdir($path,0777,true);  
        }
        $file = $path.$fileName;
        $objWriter->save($file);
        
        $file_name = sprintf("%s-%s.xlsx","invoice",$model->code);


        $model->download_time = date("Y-m-d H:i:s");

        $model->save();

        header("Content-type: application/octet-stream");    
        header("Accept-Ranges: bytes");    
        header("Accept-Length: " . filesize($file));    
        header("Content-Disposition: attachment; filename=" .$file_name);    
        @readfile($file);

    }

        
    protected function findModel($id)
    {
        if (($model = Invoice::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
